package ie.live.byrnep.pleasesleephere.base;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.simple.JSONObject;




import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

class ImageDownloader {
	public void download(String url, ImageView imageView, JSONObject json) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, json);
        task.execute(url);
    }
	
	class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
	    private String url;
	    private final WeakReference<ImageView> imageViewReference;
	    private final JSONObject imgName;

	    public BitmapDownloaderTask(ImageView imageView, JSONObject json) {
	        imageViewReference = new WeakReference<ImageView>(imageView);
	        this.imgName= json;
	    }

	    Bitmap bitmap = null;
		@Override
		protected Bitmap doInBackground(String... url) {
			try{
				bitmap = downloadUrl(url[0],imgName);
			}catch(Exception e){
				Log.d("Background Task",e.toString());
			}
			return bitmap;
		}

	    @Override
	    // Once the image is downloaded, associates it to the imageView
	    protected void onPostExecute(Bitmap bitmap) {
	        if (isCancelled()) {
	            bitmap = null;
	        }

	        if (imageViewReference != null) {
	            ImageView imageView = imageViewReference.get();
	            if (imageView != null) {
	                imageView.setImageBitmap(bitmap);
	            }
	        }
	    }
	}
	private Bitmap downloadUrl(String strUrl, JSONObject imgName) throws IOException{
		Bitmap bitmap=null;
		InputStream iStream = null;
		String sentMessage;
		try{
			//URL url = new URL(strUrl);
			/** Creating an http connection to communcate with url */
			//HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			
			 //final String stringUrl = "http://c00130348pic.appspot.com/userdetails";
			// final String PIC_KEY="pic_key";	
			 URL url = new URL(strUrl);
			 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			 JSONObject json = new JSONObject();
			//json.put(PIC_KEY, "paul");
			
			urlConnection.setDoOutput(true);
			urlConnection.setRequestProperty("Content-Type", "application/json");
			urlConnection.setRequestProperty("Accept", "application/json");
			urlConnection.setRequestMethod("POST");
			sentMessage = imgName.toString();
			final OutputStreamWriter osw = new OutputStreamWriter(
					urlConnection.getOutputStream());
			osw.write(sentMessage);
			osw.close();
			
			

	/** Connecting to url */
			//urlConnection.connect();

			/** Reading data from url */
			if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				iStream = urlConnection.getInputStream();
				int er = 4;
				int w = 3;
				er += w;
			}
			

			/** Creating a bitmap from the stream returned from the url */
			bitmap = BitmapFactory.decodeStream(iStream);

		}catch(Exception e){
			Log.d("Exception while downloading url", e.toString());
		}finally{
			iStream.close();
		}
		return bitmap;
	}
}

